﻿using ExcelDna.Integration;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace ExcelDNAForWPSDemo
{
    public class Udf
    {

        [ExcelFunction(Category = "EasyColorTranslate", Description = "不同颜色间表示方法间的转换。EasyShu出品，必属精品！")]
        public static object Easy颜色RGB转换HEX(
                  [ExcelArgument(Description = "输入RGB值,不种颜色间用逗号间隔")] string inputRGB
                   )
        {
            try
            {
                string[] splitStrs = inputRGB.Split(new char[] { ',', '，' });
                if (splitStrs.Length == 3 && splitStrs.All(s => int.TryParse(s, out int result) && result >= 0 && result <= 255))
                {
                    return ColorTranslator.ToHtml(Color.FromArgb(int.Parse(splitStrs[0]), int.Parse(splitStrs[1]), int.Parse(splitStrs[2])));
                }
                else
                {
                    return ExcelError.ExcelErrorNA;
                }

            }
            catch (Exception)
            {
                return ExcelError.ExcelErrorNA;
            }
        }

        [ExcelFunction(Category = "EasyColorTranslate", Description = "不同颜色间表示方法间的转换。EasyShu出品，必属精品！")]
        public static object Easy颜色RGB转换HEX2(
                    [ExcelArgument(Description = "输入R值，范围0-255")] object inputR,
                    [ExcelArgument(Description = "输入G值，范围0-255")] object inputG,
                    [ExcelArgument(Description = "输入B值，范围0-255")] object inputB
                     )
        {
            try
            {
                int R = Convert.ToInt32(inputR.ToString().Trim());
                int G = Convert.ToInt32(inputG.ToString().Trim());
                int B = Convert.ToInt32(inputB.ToString().Trim());

                return ColorTranslator.ToHtml(Color.FromArgb(255, R, G, B));

            }
            catch (Exception)
            {
                return ExcelError.ExcelErrorNA;
            }



        }

        [ExcelFunction(Category = "EasyColorTranslate", Description = "不同颜色间表示方法间的转换。EasyShu出品，必属精品！")]
        public static object Easy颜色RGB转换Ole(
               [ExcelArgument(Description = "输入RGB值,不种颜色间用逗号间隔")] string inputRGB
                 )
        {
            try
            {
                string[] splitStrs = inputRGB.Split(new char[] { ',', '，' });
                if (splitStrs.Length == 3 && splitStrs.All(s => int.TryParse(s, out int result) && result >= 0 && result <= 255))
                {
                    return ColorTranslator.ToHtml(Color.FromArgb(int.Parse(splitStrs[0]), int.Parse(splitStrs[1]), int.Parse(splitStrs[2])));
                }
                else
                {
                    return ExcelError.ExcelErrorNA;
                }

            }
            catch (Exception)
            {
                return ExcelError.ExcelErrorNA;
            }
        }

        [ExcelFunction(Category = "EasyColorTranslate", Description = "不同颜色间表示方法间的转换。EasyShu出品，必属精品！")]
        public static object Easy颜色RGB转换Ole2(
            [ExcelArgument(Description = "输入R值，范围0-255")] object inputR,
            [ExcelArgument(Description = "输入G值，范围0-255")] object inputG,
            [ExcelArgument(Description = "输入B值，范围0-255")] object inputB
             )
        {
            try
            {
                int R = Convert.ToInt32(inputR.ToString().Trim());
                int G = Convert.ToInt32(inputG.ToString().Trim());
                int B = Convert.ToInt32(inputB.ToString().Trim());

                return ColorTranslator.ToOle(Color.FromArgb(255, R, G, B));
            }
            catch (Exception)
            {
                return ExcelError.ExcelErrorNA;
            }
        }

        [ExcelFunction(Category = "EasyColorTranslate", Description = "不同颜色间表示方法间的转换。EasyShu出品，必属精品！")]
        public static object Easy颜色Ole转换RGB(
                [ExcelArgument(Description = "输入Ole值，OFFICE软件的Color属性")] int inputOle
                     )
        {
            try
            {
                Color color = ColorTranslator.FromOle(inputOle);
                return $"{color.R},{color.G},{color.B}";
            }
            catch (Exception)
            {
                return ExcelError.ExcelErrorNA;
            }
        }

        [ExcelFunction(Category = "EasyColorTranslate", Description = "不同颜色间表示方法间的转换。EasyShu出品，必属精品！")]
        public static object Easy颜色Ole转换HEX(
        [ExcelArgument(Description = "输入Ole值，OFFICE软件的Color属性")] int inputOle
             )
        {
            try
            {
                Color color = ColorTranslator.FromOle(inputOle);

                return ColorTranslator.ToHtml(Color.FromArgb(255, color.R, color.G, color.B));
            }
            catch (Exception)
            {
                return ExcelError.ExcelErrorNA;
            }
        }

        [ExcelFunction(Category = "EasyColorTranslate", Description = "不同颜色间表示方法间的转换。EasyShu出品，必属精品！")]
        public static object Easy颜色HEX转换RGB(
                [ExcelArgument(Description = "输入网页HEX格式颜色值，由#开头")] string inputHEXColor
                         )
        {
            try
            {
                Color color = ColorTranslator.FromHtml(inputHEXColor);
                return $"{color.R},{color.G},{color.B}";
            }
            catch (Exception)
            {
                return ExcelError.ExcelErrorNA;
            }
        }

        [ExcelFunction(Category = "EasyColorTranslate", Description = "不同颜色间表示方法间的转换。EasyShu出品，必属精品！")]
        public static object Easy颜色HEX转换Ole(
            [ExcelArgument(Description = "输入网页HEX格式颜色值，由#开头")] string inputHEXColor
         )
        {
            try
            {
                Color color = ColorTranslator.FromHtml(inputHEXColor);
                return ColorTranslator.ToOle(color);
            }
            catch (Exception)
            {
                return ExcelError.ExcelErrorNA;
            }
        }
    }
}
